// components/product-coupon-pop/index.js
import {
  queryProductCouponList,
  receiveCoupon,
} from "../../service/jb_coupon_service.js"
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
    },
    productId: {
      type: String,
      optionalTypes: [Number, String],
      value: '0'
    },
    goodType: {
      type: String,
      value: "normal"
    },
  },
  observers: {
    'show': function (value) {
      this.setData({
        showPop: value
      })
    },
    'productId': function (value) {
      this.queryProductCouponList(false);
    }

  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行


    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    showPop: false,
    couponType: 1,
    overlayStyle: 'background-color: rgba(0, 0, 0, 0.25);',
    couponList: [],

    tabs: [
      { id: '1', title: '通用券', loaded: false },
      { id: '2', title: '商品券', loaded: false },
      { id: '3', title: '品类券', loaded: false },
      // { id: 'c', title: '已过期', loaded: false }
    ],
    topCategoryIndex: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    clickCenterPopupOverlay() {
      this.setData({
        showPop: false,
      })
      this.triggerEvent('closeSelectPop')
    },
    //商品中查询优惠券
    queryProductCouponList(loadType) {
      if (this.properties.goodType == "seckill") {
        return;
      }
      if (loadType) {
        wx.showLoading();
      }
      queryProductCouponList(this.properties.productId, this.data.couponType).then(res => {
        wx.hideLoading();
        if (res.code == 200) {
          this.setData({
            couponList: res.data.list || []
          })
        }
      }).catch(err => {
        wx.hideLoading();
        console.error("JJLog:queryProductCouponList", err);
      })
    },
    topCategoryChange(e) {
      let index = e.currentTarget.dataset.index;
      let item = this.data.tabs[index]
      this.setData({
        couponType: item.id,
        topCategoryIndex: index,
      })
      this.queryProductCouponList(true);
    },
    //点击一下：
    getCouponAction(e) {
      console.log("JJLog:看那看这是：" + JSON.stringify(e));
      if (!app.globalData.isLogin) {
        wx.navigateTo({
          url: '/pages/login/login',
        })
        return;
      }
      let couponId = e.detail.couponId;
      let index = e.detail.index;
      let changeItem = this.data.couponList[index];
      wx.showLoading();
      receiveCoupon({ couponId: couponId }).then(res => {
        wx.hideLoading();
        if (res.code == 200) {
          wx.showToast({
            title: "领取成功",
            icon: "success"
          })
          changeItem.isUse = true;
          let item = 'couponList[' + index + ']';
          this.setData({
            [item]: changeItem,
          })

        }
      }).catch(err => {
        wx.hideLoading();
        console.error("JJLog:receiveCoupon", err)
        wx.showToast({
          title: err || '领取失败',
          icon: "none"
        })
      })
    },

  },


})
